package com.SDR;

import com.SDR.Dao.Impl.IpRedisDaoImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * Created by Yoey on 2017/6/26.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:applicationContext.xml"})
public class TestSDR {
    @Autowired
    private RedisTemplate<String,Serializable> redisTemplate;

    @Autowired
    private IpRedisDaoImpl ipRedisDaoImpl;

    //向Redis中存放数据,修改Redis中的数据也是直接调用这个add方法即可
    @Test
    public void  putIntoRedis(){
        redisTemplate.opsForValue().set("User","AAAA--");
    }

    //使用FastJSon序列化
  /*  @Test
    public void  putIntoRedis2(){
        User user=new User("AA","123",123.45);
        Object result=JSON.toJSONString(user);
        redisTemplate.opsForSet().add("UserSet",result);
    }*/
    @Test
    public void putIntoRedis3(){
        User user=new User("AA","123",123.45);
        ValueOperations<String, Serializable> valueOperations = redisTemplate.opsForValue();
        redisTemplate.opsForValue().set("UserBySer",user);
    }
    //从Redis中获取数据
    @Test
    public void getByRedis(){
        User user= (User) redisTemplate.opsForValue().get("UserBySer");     //--->解析序列化后的
        System.out.println(user);
    }
    @Test
    public void  getByRedis2(){
        System.out.println(redisTemplate.opsForValue().get("User"));
    }

    //对对象设置过期时间
    @Test
    public void  setTTL(){
        redisTemplate.opsForValue().set("UserWithTTL","AAAA--",60, TimeUnit.SECONDS);
    }

    @Test
    public void  test1(){
        ipRedisDaoImpl.setIntoRedis("127.0.0.1","南京");
    }
    @Test
    public void  test2(){
        System.out.println(ipRedisDaoImpl.getFromRedis("127.0.0.1"));
        System.out.println(ipRedisDaoImpl.getFromRedis("UserBySer"));
    }
}
